<?php

/**
 * image form.
 *
 * @package    semramode
 * @subpackage form
 * @author     Your name here
 * @version    SVN: $Id: sfDoctrineFormTemplate.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class imageForm extends BaseimageForm
{
  public function configure()
  {
     
            $this->widgetSchema['filename'] =  new sfWidgetFormInputFileEditable(array(
  'label' => 'Productfoto',
   'file_src'    => '/uploads/images/tn_'.$this->getObject()->filename,
    'edit_mode'   => true,
    'is_image'    => true,
    'with_delete' => false,
));
      $this->validatorSchema['filename'] =  new sfValidatorFile(array(
  'required'   => false,
  'path'       => sfConfig::get('sf_upload_dir').'/images',
  'mime_types' => 'web_images',
));
     $this->widgetSchema['product_id'] = new sfWidgetFormInputHidden();
if(!$this->isNew()){
     $this->setWidget('delete', new sfWidgetFormInputDelete(array(
   'url' => 'producten/deleteImage',      // required
    'model_id' => $this->getObject()->id,        // required
    'confirm' => 'Sure???',                     // optional
    )));
}
//
  }

protected function doSave( $con = null){

$parent = parent::doSave($con);
   $file = $this->getObject()->filename;
   if($file !=''){
   $filename = sha1($file->getOriginalName()).$file->getExtension($file->getOriginalExtension());
    $file->save(sfConfig::get('sf_upload_dir').'/'.$filename);


    $image = new image();

 $image->createthumb(sfConfig::get('sf_upload_dir').'/images/'.$filename ,sfConfig::get('sf_upload_dir').'/images/tn_'.$filename,182,213);

   

   }
  return $parent;
}
public function updateObject($values = null)
  {
  
    
     if($values['id'] == ''){
    $values['id'] = $this->getObject()->getId();
     }
     $object = parent::updateObject($values);
    if($values['filename'] != $this->getObject()->getFilename() && $values['filename'] != ''){

    $image = new image();

$image->createthumb(sfConfig::get('sf_upload_dir').'/images/'.$object->getFilename() ,sfConfig::get('sf_upload_dir').'/images/tn_'.$object->getFilename(),182,213);
    }

    return $object;

  
  }
}